package com.first.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.common.pojo.AclRole;
import com.first.common.vo.Result;
import com.first.system.service.RoleService;
import com.first.system.vo.AddRoleVo;
import com.first.system.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: finrsteducation-parent
 * @description: 角色
 * @author: 张玉森
 * @create: 2023-05-26 16:39
 **/
@RestController
@RequestMapping("system/role")
@Api(tags = "角色管理接口")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("updateRoleStatusByid/{id}/{deleted}")
    @ApiOperation("修改角色状态")
    public Result updateRole(@PathVariable String id,
                             @PathVariable String deleted) {
//        System.out.println(id);
//        System.out.println(deleted);
        Integer update = roleService.updateRoleId(id, deleted);
        if (update > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    @DeleteMapping("deleteRoleById/{id}")
    @ApiOperation("角色删除")
    public Result deleteRoleById(@PathVariable String id) {
        Integer delete= roleService.deleteRoleById(id);
        if (delete>0){
            return Result.success();
        }
        return Result.fail("删除失败");
    }

    @PostMapping("findRoleByPage/{page}/{pageSize}")
    @ApiOperation("角色条件分页查询")
    public Result findRolePAge(@PathVariable String page,
                               @PathVariable String pageSize,
                               @RequestBody RoleVo roleVo) {
//        System.out.println(page);
//        System.out.println(pageSize);
//        System.out.println(roleVo);
        Page<AclRole> aclRolePage = roleService.findRolePage(page, pageSize, roleVo);
        return Result.success(aclRolePage);
    }

    @GetMapping("getByUserId/{userId}")
    @ApiOperation("用户获ID取角色")
    public Result getByUserId(@PathVariable String userId) {
        List<AclRole> all = roleService.findAll();
        List<AclRole> roles = roleService.findByUserId(userId);
        List<String> roleids = roles.stream().map(item -> item.getId()).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("allRole", all);
        map.put("userRoleIds", roleids);
        return Result.success(map);
    }

    @PostMapping("confirm")
    @ApiOperation("确认分配角色")
    public Result confirm(String userId, String[] roleIds) {
        roleService.confirm(userId, roleIds);
        return Result.success();
    }

    @PostMapping("addOrUpdateRole")
    @ApiOperation("添加角色")
    public Result adduser(@RequestBody AddRoleVo addRoleVo) {
        //System.out.println(addRoleVo);
        Integer addnum = roleService.addUser(addRoleVo);
        if (addnum > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    @PostMapping("updateOrUpdateRole")
    @ApiOperation("修改角色信息")
    public Result updateuser(@RequestBody AclRole aclRole) {
        Integer addnum = roleService.updateUser(aclRole);
        if (addnum > 0) {
            return Result.success();
        }
        return Result.fail();
    }
}
